package com.wenlin.myproduction.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.RecyclerView;

import com.wenlin.myproduction.R;
import com.wenlin.myproduction.base.BaseSwipeActivity;
import com.wenlin.myproduction.bean.RecordBean;
import com.wenlin.myproduction.bean.Records;
import com.wenlin.myproduction.binder.RecordBinder;
import com.wenlin.myproduction.util.Checker;
import com.wenlin.myproduction.util.MyGridLayoutManager;
import com.wenlin.myproduction.util.TypeData;
import com.wenlin.myproduction.util.TypeDataNames;
import com.wenlin.myproduction.util.ValCommon;
import com.wenlin.myproduction.widget.ShuDuView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class ShuDuActivity extends BaseSwipeActivity {
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.shu_du_view)
    ShuDuView sdv;

    private Items items = new Items();
    private MultiTypeAdapter adapter;

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, ShuDuActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_shu_du;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(ValCommon.SHU_DU_VIEW);
        adapter = new MultiTypeAdapter(items);
        adapter.register(RecordBean.class, new RecordBinder());
        rv.setLayoutManager(new MyGridLayoutManager(this, 1));
        rv.setHasFixedSize(true);
        rv.setAdapter(adapter);
        sdv.setEdit(true);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void threadHandle(TypeData typeData) {
        String name = typeData.getName();
        if (name.equals(TypeDataNames.setRecord)) {
            Records records = (Records) typeData.getData();
            if (Checker.isNull(records)) return;
            if (Checker.hasList(items)) {
                items.clear();
                adapter.notifyDataSetChanged();
            }
            items.addAll(records.getList());
            adapter.notifyDataSetChanged();
        }
        if (name.equals(TypeDataNames.resetToRecord)) {
            RecordBean recordBean = (RecordBean) typeData.getData();
            if (Checker.isNull(recordBean)) return;
            List<RecordBean> list = new ArrayList<>();
            if (Checker.hasList(items)) {
                for (int i = 0; i < items.size(); i++) {
                    Object o = items.get(i);
                    if (o instanceof RecordBean) {
                        RecordBean rb = (RecordBean) o;
                        list.add(rb);
                        if (rb.getStep() == recordBean.getStep()) {
                            break;
                        }
                    }
                }
                items.clear();
                adapter.notifyDataSetChanged();
            }
            items.addAll(list);
            adapter.notifyDataSetChanged();
            sdv.setData(recordBean);
            sdv.setRecords(list);
        }
    }
}
